package leetcode.sort;

import java.util.Arrays;

public abstract class AbstractSort {
    protected int[] array;

    public AbstractSort(int[] array) {
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException("数组不能为空");
        }
        this.array = array;
        long begin = System.currentTimeMillis();
        this.sort();
        long end = System.currentTimeMillis();
        String simpleName = this.getClass().getSimpleName();
        System.out.println(simpleName + "耗时：" + (end - begin) + "ms");
    }

    protected abstract void sort();

    protected void swap(int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "{" +
                "array=" + Arrays.toString(array) +
                '}';
    }
}
